import { Graph } from '@antv/x6'
import { DataType } from '../../../shapes/type'

Graph.registerNode('IndicatorLight', {
  inherit: 'BaseImage',
  attrs: {
    image: {
      href: new URL('./images/3dRedBtn.svg', import.meta.url).toString()
    }
  },
  data: {
    attrRoot: 'image',
    dataConfig: {
      runModel: 'variableValue',
      deviceId: null,
      variableId: null,
      variableAddress: null,
      bindConfig: [
        { operate: '==', value: 1, href: new URL('./images/3dGreenBtn.svg', import.meta.url).toString() },
        { operate: '!=', value: 1, href: new URL('./images/3dRedBtn.svg', import.meta.url).toString() }
      ],
      updateData (cell, data: any) {
        const bindConfig = cell.data.dataConfig.bindConfig
        let hrefValue = null
        try {
          for (let i = 0, len = bindConfig.length; i < len; i++) {
            const item = bindConfig[i]
            // eslint-disable-next-line no-eval
            if (eval(data.value + item.operate + item.value)) {
              hrefValue = item.href
              break
            }
          }
        } catch (e) {
        }
        cell.attr('image/href', hrefValue)
      },
      defaultValue ({ cell }) {
        cell.attr('image/href', cell.data.dataConfig.bindConfig[0]?.href)
      }
    },
    events: {
      // added (graph, { cell }) {
      //   console.log(graph, cell)
      //   cell.data.dataConfig.updateData(cell, { value: 0 })
      // }
    },
    getAttrModules: () => {
      return [
        {
          title: '绑定设备变量',
          group: 'Data',
          components: () => import('../../components/BindDeviceVariable.vue'),
          solution: {}
        },
        {
          title: '更多',
          group: 'Data',
          components: () => import('./components/More.vue'),
          solution: {}
        }
      ]
    }
  } as DataType
} as any)
